import { JobType } from "@/api";

export function buildTree(data: JobType[], rootId = 0) {
  // 创建ID到节点的映射
  const nodeMap = {};
  data.forEach((item: JobType) => {
    nodeMap[item.id!] = { ...item, children: [] };
  });

  // 构建树结构
  const tree = [] as any[];
  data.forEach((item: JobType) => {
    if (item.parentId === rootId) {
      tree.push(nodeMap[item.id!]);
    } else {
      const parent = nodeMap[item.parentId!];
      if (parent) {
        parent.children.push(nodeMap[item.id!]);
      }
    }
  });

  return tree;
}
